package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.controller.public_api_i.IPublicApiSunPanelSystemStatus;
import com.ruoyi.system.entity.*;
import com.ruoyi.system.service.IDockerService;
import com.ruoyi.system.service.IPveService;
import com.ruoyi.system.service.IUpsService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/public/api")
public class IndsvrPublicApiController extends BaseController implements IPublicApiSunPanelSystemStatus
{

    @Resource
    private IPveService pveService;

    @Resource
    private IDockerService dockerService;


    @Resource
    private IUpsService upsService;


    // Sun-Panel 接口如下

    //  /api/system 代理到 /public/api/sunpanel/system
    //sunpanel cpu信息
    @RequestMapping(value = "/sunpanel/system/monitor/getCpuState")
    public AjaxResult getCpuState() throws Exception
    {
        SunPanelCPU cpuInfo = pveService.getCpuState();
        return AjaxResult.success2Pve("成功",cpuInfo);
    }

    //sunpanel 内存
    @RequestMapping(value = "/sunpanel/system/monitor/getMemonyState")
    public AjaxResult getMemonyState() throws Exception
    {
        SunPanelMemony memonyState = pveService.getMemonyState();
        return AjaxResult.success2Pve("成功",memonyState);
    }

    //sunpanel 硬盘
    @RequestMapping(value = "/sunpanel/system/monitor/getDiskStateByPath")
    public AjaxResult getDiskStateByPath(@RequestBody SunRequestBody requestBody) throws Exception
    {
        SunPanelDisk diskStateByPath = pveService.getDiskStateByPath(requestBody.getPath());

        return AjaxResult.success2Pve("成功",diskStateByPath);
    }
    //sunpanel 硬盘列表
    @RequestMapping(value = "/sunpanel/system/monitor/getDiskMountpoints")
    public AjaxResult getDiskMountpoints() throws Exception
    {
        ArrayList<SunPanelDiskList> diskMountpoints = pveService.getDiskMountpoints();
        return AjaxResult.success2Pve("成功",diskMountpoints);
    }



    // /api/docker 代理到 /public/api/sunpanel/docker
    //sunpanel 容器状态
    @RequestMapping(value = "/sunpanel/docker/container/getContainerStatus")
    public AjaxResult getContainerStatus(@RequestBody SunRequestBody sunRequestBody) throws Exception
    {
        ArrayList<String> ids = new ArrayList<>();

        HashMap<String, SunPanelDockerStatus> containersStats = new HashMap<>();

        String[] split = sunRequestBody.getId().split("-");
        if (split[0].equals("pve")) {
            ids.add(split[2]);
            if ("lxc".equals(split[1])) {
                containersStats = pveService.getLxcStats(ids);
            }else if ("vm".equals(split[1])) {
                containersStats = pveService.getQemuStats(ids);
            }
        }else {
            ids.add(sunRequestBody.getId());
            containersStats = dockerService.getContainersStats(ids);
        }

        return AjaxResult.success2Pve("成功",containersStats.get(sunRequestBody.getId()));
    }
    @RequestMapping(value = "/sunpanel/docker/container/getContainersStats")
    public AjaxResult getContainersStats(@RequestBody List<String> ids) throws Exception
    {
        HashMap<String, SunPanelDockerStatus> containersStats = new HashMap<>();

        ArrayList<String> lxcId = new ArrayList<>();
        ArrayList<String> vmId = new ArrayList<>();
        ArrayList<String> dockerId = new ArrayList<>();

        for (String id : ids) {
            String[] split = id.split("-");
            if (split[0].equals("pve")) {
                if ("lxc".equals(split[1])) {
                    lxcId.add(split[2]);
                }else if ("vm".equals(split[1])) {
                    vmId.add(split[2]);
                }
            }else {
                dockerId.add(id);
            }
        }

        containersStats.putAll(pveService.getLxcStats(lxcId));
        containersStats.putAll(pveService.getQemuStats(vmId));
        containersStats.putAll(dockerService.getContainersStats(dockerId));

        return AjaxResult.success2Pve("成功",containersStats);
    }


    //sunpanel 容器状态切换
    @RequestMapping(value = "/sunpanel/docker/container/execSwitchActionByContainerID")
    public AjaxResult execSwitchActionByContainerID(@RequestBody SunRequestBody sunRequestBody) throws Exception
    {
        String[] split = sunRequestBody.getId().split("-");
        if (split[0].equals("pve")) {
            if ("lxc".equals(split[1])) {
                pveService.execLxcSwitchActionByContainerID(sunRequestBody.isAction(),split[2]);
            }else if ("vm".equals(split[1])) {
                pveService.execVmSwitchActionByContainerID(sunRequestBody.isAction(),split[2]);
            }
        }else {
            dockerService.execSwitchActionByContainerID(sunRequestBody.isAction(),sunRequestBody.getId());
        }

        return AjaxResult.success2Pve("成功",null);
    }

    //sunpanel 容器列表
    @RequestMapping(value = "/sunpanel/docker/container/getList")
    public AjaxResult getList() throws Exception
    {
        SunPanelDockerList list = dockerService.getList();


        //id   pve-lxc-100
        SunPanelDockerList list2 =  pveService.getLxcAndQemuList();
        list.setCount(list.getCount()+list2.getCount());
        list.getList().addAll(list2.getList());

        return AjaxResult.success2Pve("成功",list);
    }






}
